<?php

namespace app\fly_pig\controller;

class FlyPigOrderRefund
{

    public $count = 1;
    /**
     * 构造函数
     * MfwComment constructor.
     */
    public function __construct(){}

    /**
     *  子进程启动时触发
     * @param $worker
     */
    public function onWorkerStart($worker){
        echo "飞猪退款订单子进程启动了\n";
       if ($worker->id === 0){
           \workerman\Lib\Timer::add(30, [$this,'dealDate']);
       }elseif ($worker->id === 1){
           \workerman\Lib\Timer::add(100, [$this, 'timeTask']);
       }
    }

    /**
     * 连接时触发
     * @param $connection
     */
    public function onConnect($connection){
        echo "连接成功\n";
        $connection->send('<h2 style="color: #4288ce;font-size: 20px">hello!</h2>');
    }

    /**
     *  接收到消息时触发
     * @param $connection
     * @param $message
     */
    public function onMessage($connection, $message) {
        var_dump($message);
        $connection->send("<span style='color: #000088;'>很高兴和你认识</span>\n");
    }

    /**
     *  连接关闭时触发
     * @param $connection
     */
    public function onClose($connection){
        echo "连接断开了\n";
    }

    /**
     *  worker进程停止时触发
     * @param $connection
     */
    public function onWorkerStop($connection){
        echo "正在重启!\n";
    }


    /**
     *  从马蜂窝平台获取订单信息
     */
    public function timeTask()
    {
        echo "定时任务\n";
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        $date = $redis->lPop('order_refund_date');
        if (!empty($date)){
            $flyPigObj = new FlyPig();
            $flyPigObj->getFlyPigOrderRefund($date); // 根据推送创建时间来获取飞猪退款订单
        }
        // 根据推送修改时间获取
        $tui = $redis->lPop('order_refund_modify_date');
        if (!empty($tui)){
            $flyPigObj = new FlyPig();
            $flyPigObj->getFlyPigOrderRefund('', $tui); // 根据推送修改时间来获取飞猪退款订单
        }
        // 处理多余的日期
        $this->delDate();
    }


    /**
     *  处理日期
     */
    public function dealDate()
    {
        // 根据当前时间生成7天之前的日期
        $date = date('Y-m-d',time());
        $dates = $this->getDates($date);
        if (!empty($dates)){
            $this->getPushAddTime($dates);
            $this->getPushEditTime($dates);
        }
    }

    /**
     *  获取前10天的日期
     */
    public function getDates($value = '')
    {
        $arr = [];
        $day = 30;
        $arr[] = $value;
        if (!empty($value)){
            for ($i = 1; $i < $day; $i++) {
                $arr[] = date('Y-m-d', strtotime("{$value} -{$i}day"));
            }
        }
        return $arr;
    }

    /**
     *  删除超过15天的日期
     */
    public function delDate()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->select(6);
        $date = date('Y-m-d', time());
        $dates = $this->getDates($date);
        $orderRefundDate1 = $redis->lRange('order_refund_date1', 0, -1);
        if (!empty($orderRefundDate1)){
            foreach ($orderRefundDate1 as $index => $item) {
                if (!in_array($item, $dates)){
                    $redis->lRem('order_refund_date1',  $item, 0);
                }
            }
        }
        $orderRefundDate2 = $redis->lRange('order_refund_modify_date1', 0, -1);
        if (!empty($orderRefundDate2)){
            foreach ($orderRefundDate2 as $index => $item) {
                if (!in_array($item, $dates)){
                    $redis->lRem('order_refund_modify_date1',  $item, 0);
                }
            }
        }
    }
    
    // 获取订单推送创建时间
    public function getPushAddTime($dates = [])
    {
        if (!empty($dates)){
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);
            $redis->select(6);
            // 从redis获取数据
            $data1 = $redis->lRange('order_refund_date', 0 , -1);
            $data2 = $redis->lRange('order_refund_date1',0, -1);
            $data = [];
            if (!empty($data1) && !empty($data2)) {
                $dateData1 = [];
                foreach ($data1 as $index => $item) {
                    array_push($dateData1, substr($item, 0 ,10));
                }
                $data = array_unique(array_merge($dateData1, $data2));
            }elseif (!empty($data1) && empty($data2)){
                $dateData1 = [];
                foreach ($data1 as $index => $item) {
                    array_push($dateData1, substr($item, 0 ,10));
                }
                $data = $dateData1;
            }elseif (empty($data1) && !empty($data2)){
                $data = $data2;
            }
            if (!empty($data)){
                foreach ($dates as $index1 => $datum) {
                    if (!in_array($datum, $data)){
                        $redis->rPush('order_refund_date', $datum);
                    }
                }
            }else{
                foreach ($dates as $index2 => $datum2) {
                    $redis->rPush('order_refund_date', $datum2);
                }
            }
        }
    }

    // 获取订单推送修改时间
    public function getPushEditTime($dates = [])
    {
        if (!empty($dates)){
            $redis = new \Redis();
            $redis->connect('127.0.0.1', 6379);
            $redis->select(6);
            // 从redis获取数据
            $data1 = $redis->lRange('order_refund_modify_date', 0 , -1);
            $data2 = $redis->lRange('order_refund_modify_date1',0, -1);
            $data = [];
            if (!empty($data1) && !empty($data2)) {
                $dateData1 = [];
                foreach ($data1 as $index => $item) {
                    array_push($dateData1, substr($item, 0 ,10));
                }
                $data = array_unique(array_merge($dateData1, $data2));
            }elseif (!empty($data1) && empty($data2)){
                $dateData1 = [];
                foreach ($data1 as $index => $item) {
                    array_push($dateData1, substr($item, 0 ,10));
                }
                $data = $dateData1;
            }elseif (empty($data1) && !empty($data2)){
                $data = $data2;
            }
            if (!empty($data)){
                foreach ($dates as $index1 => $datum) {
                    if (!in_array($datum, $data)){
                        $redis->rPush('order_refund_modify_date', $datum);
                    }
                }
            }else{
                foreach ($dates as $index2 => $datum2) {
                    $redis->rPush('order_refund_modify_date', $datum2);
                }
            }
        }
    }
}